//
//  HSTabBarView.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 15/10/31.
//  Copyright © 2015年 huanghaisheng. All rights reserved.
//

import UIKit

fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func >= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l >= r
  default:
    return !(lhs < rhs)
  }
}


@objc public protocol HSTabBarViewDelegate: NSObjectProtocol {

    @objc optional func tabBarView(_ tabBarView: HSTabBarView, didSelectAtIndex index: Int)
    @objc optional func tabBarView(_ tabBarView: HSTabBarView, shouldSelectAtIndex index: Int) -> Bool
}

@objc open class HSTabBarView: UIView, HSTabBarItemViewDelegate  {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

    var tarBarInfos: NSArray?
    var topLineView: UIImageView?
    var selectedTabBarItem: HSTabBarItemView?
    var delegate: HSTabBarViewDelegate?
    var selectedIndex: Int?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        self.delegate = nil
    }
    
    public init(frame: CGRect, items: NSArray, barStyle: HSTabItemStyle, selectedIndex: Int) {
        
        super.init(frame: frame)
        
        self.loadCustomTabBarView(items, barStyle: barStyle, selectedIndex: selectedIndex)
        
    }
    
    public convenience init(frame: CGRect, items: NSArray, barStyle: HSTabItemStyle, selectedIndex: Int, test:Bool) {
        
        self.init(frame: frame)
        
        self.loadCustomTabBarView(items, barStyle: barStyle, selectedIndex: selectedIndex)
    }
    
    /*检测选择中的index是否有效，无效则重置为0*/
    fileprivate func validDateIndex(_ index: Int) -> Int {
        
        if (index < 0) || (index >= self.tarBarInfos?.count) {
            return 0
        }
        return index
    }
    
    open func shouldClickedTabBarItemView(_ view: HSTabBarItemView) -> Bool {
        
        if view .isEqual(self.selectedTabBarItem) {
            return false
        } else {
            let sel = #selector(HSTabBarViewDelegate.tabBarView(_:shouldSelectAtIndex:))
            if ((self.delegate?.responds(to: sel)) != false) {
                return (self.delegate?.tabBarView!(self, shouldSelectAtIndex: view.tag))!
            } else {
                return true
            }
            
        }
    }
    
    open func didClickedTabBarItemView(_ view: HSTabBarItemView) {
        
        if view .isEqual(self.selectedTabBarItem) {
            return
        } else {
            self.selectedTabBarItem?.setNormalStatus()
            self.selectedTabBarItem = view
            self.delegate?.tabBarView!(self, didSelectAtIndex: view.tag)
        }
    }
    
    open func setBarItemHighlightedAtIndex(_ index: Int) {
        
        for itemView in self.subviews {
            
            if itemView is HSTabBarItemView {
                
                if itemView.tag == index {
                    (itemView as! HSTabBarItemView).setHighlightedStatus()
                    self.selectedTabBarItem = itemView as? HSTabBarItemView
                } else {
                    (itemView as! HSTabBarItemView).setNormalStatus()
                }
            }
        }
    }
    
    open func clickAtIndex(_ index: Int) {
    
        for itemView in self.subviews {
            
            if itemView is HSTabBarItemView {
                
                if itemView.tag == index {
                    (itemView as! HSTabBarItemView).clickMe()
                }
            }
        }
    }
    
    open func descriptionThisView() {
        
        print("TabBarView")
    }
    
    open func loadCustomTabBarView(_ items: NSArray, barStyle: HSTabItemStyle, selectedIndex: Int) {
        
        self.tarBarInfos = items
        self.selectedIndex = selectedIndex
        
        /*加顶部灰色线*/
        let topLineView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: frame.width, height: 0.5))
        topLineView.backgroundColor = UIColor(white: 216.0/255.0, alpha: 1.0)
        self.addSubview(topLineView)
        
        let itemWidth:CGFloat = frame.width/CGFloat(items.count)
        /*添加Bar Item*/
        for i in 0...items.count-1 {
            
            let tempObj = items[i]
            let itemView: HSTabBarItemView
            let itemFrame: CGRect = CGRect(x: itemWidth*CGFloat(i), y: 0.0, width: itemWidth, height: frame.height)
            
            if tempObj is HSTabBarItemView {
                itemView = tempObj as! HSTabBarItemView
                itemView.frame = itemFrame
            } else {
                itemView = HSTabBarItemView(frame: itemFrame, itemInfo: tempObj as? NSDictionary, itemStyle: barStyle)
            }
            itemView.delegate = self
            itemView.tag = i
            self.addSubview(itemView)
            
            if i == selectedIndex {
                self.selectedTabBarItem = itemView
                itemView.setHighlightedStatus()
            }
        }
    }
    
}
